package utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
import org.xmlpull.v1.XmlSerializer;
import Entity.SupEntity;
import android.os.Environment;
import android.util.Log;
import android.util.Xml;


public class IOHandler {

	public static void InputStreamToFile(InputStream is, String name) {
		OutputStream outputStream = null;
		File pFile = null;
		
		try {
			
				
			pFile = new File(IOHandler.getFilePath(name));
				pFile.createNewFile();
			
			outputStream = new FileOutputStream(pFile);
			int read = 0;
			byte[] bytes = new byte[1024];

			while ((read = is.read(bytes)) != -1) {
				outputStream.write(bytes, 0, read);
			}
			
			Log.d("Save to Storage", "Done");

		} catch (IOException e) {
			e.printStackTrace();
			is = null;
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (outputStream != null) {
				try {
					outputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static InputStream FileToInputStream(String name) {
		InputStream inputStream = null;
		try {
			inputStream = new FileInputStream(IOHandler.getFilePath(name));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		}
		return inputStream;
	}

	public static boolean checkFile(String ...name) {	
		for (int i = 0 ; i <name.length ; i++){
			File pFile = new File(IOHandler.getFilePath(name[i]+".xml"));
			if (pFile.exists()) {
				continue;
			}	
			else{
		     return false;}
			}
		return true;
	}
	
	public static String getFilePath(String name){
		String state = Environment.getExternalStorageState();
		if (Environment.MEDIA_MOUNTED.equals(state)) {
			File pPath = Environment.getExternalStorageDirectory();
			if (!(new File(pPath.getAbsolutePath() + "/Suppliers").exists())) {
				Log.d("IO", "Directory was created");
				new File(pPath.getAbsolutePath() + "/Suppliers").mkdir();
			}
			return pPath.getAbsolutePath() + "/Suppliers/"+ name;
			
		}
		return null;
	}
	
	public static void copyFile(String sourceFile, String destFile) throws IOException {
	    if(! (new File(IOHandler.getFilePath(destFile)).exists())) {
	    	new File(IOHandler.getFilePath(destFile)).createNewFile();
	    }

	    FileChannel source = null;
	    FileChannel destination = null;

	    try {
	        source = new FileInputStream(IOHandler.getFilePath(sourceFile)).getChannel();
	        destination = new FileOutputStream(IOHandler.getFilePath(destFile)).getChannel();
	        destination.transferFrom(source, 0, source.size());
	    }
	    finally {
	        if(source != null) {
	            source.close();
	        }
	        if(destination != null) {
	            destination.close();
	        }
	    }
	}
	
	public static File createFullXml(SupEntity sup , String file) {
		FileOutputStream fileos = null;
		File xmlFile = null;	
		xmlFile = new File(IOHandler.getFilePath(file));
		try {
			fileos = new FileOutputStream(xmlFile);
		} catch (FileNotFoundException e) {
			Log.e("FileNotFoundException", e.toString());
			return null;
		}
		XmlSerializer serializer = Xml.newSerializer();
		try {
			serializer.setOutput(fileos, "UTF-8");
			serializer.startDocument(null, Boolean.valueOf(true));
			serializer.setFeature(
					"http://xmlpull.org/v1/doc/features.html#indent-output",
					true);
			serializer.startTag(null, "data");
			serializer.startTag(null, "supname");
			if (sup.getSupname() == null) {
				serializer.text("");
			} else {
				serializer.text(sup.getSupname());
			}
			serializer.endTag(null, "supname");
			serializer.startTag(null, "supcodep");
			if (sup.getSupcodep() == null) {
				serializer.text("");
			} else {
				serializer.text(sup.getSupcodep());
			}
			serializer.endTag(null, "supcodep");
			serializer.startTag(null, "supcodef");
			if (sup.getSupcodef() == null) {
				serializer.text("");
			} else {
				serializer.text(sup.getSupcodef());
			}
			serializer.endTag(null, "supcodef");
			serializer.startTag(null, "car");
			if (sup.getCar() == null) {
				serializer.text("");
			} else {
				serializer.text(sup.getCar());
			}
			serializer.endTag(null, "car");
			serializer.startTag(null, "carcodeoil");
			if (sup.getCarcodeoil() == null) {
				serializer.text("");
			} else {
				serializer.text(sup.getCarcodeoil());
			}
			serializer.endTag(null, "carcodeoil");
			serializer.startTag(null, "carcodezp");
			if (sup.getCarcodezp() == null) {
				serializer.text("");
			} else {
				serializer.text(sup.getCarcodezp());
			}
			serializer.endTag(null, "carcodezp");
			serializer.startTag(null, "statusov");
			if (sup.getStatusov() == null) {
				serializer.text("");
			} else {
				serializer.text(sup.getStatusov());
			}
			serializer.endTag(null, "statusov");
			serializer.startTag(null, "v");
			if (sup.getV() == null) {
				serializer.text("");
			} else {
				serializer.text(sup.getV());
			}
			serializer.endTag(null, "v");
			serializer.startTag(null, "r");
			if (sup.getR() == null) {
				serializer.text("");
			} else {
				serializer.text(sup.getR());
			}
			serializer.endTag(null, "r");
			serializer.startTag(null, "o");
			if (sup.getO() == null) {
				serializer.text("");
			} else {
				serializer.text(sup.getO());
			}
			serializer.endTag(null, "o");
			serializer.startTag(null, "ostatok");
			if (sup.getOstatok() == null) {
				serializer.text("");
			} else {
				serializer.text(sup.getOstatok());
			}
			serializer.endTag(null, "ostatok");
			serializer.startTag(null, "kassa");
			if (sup.getKassa() == null) {
				serializer.text("");
			} else {
				serializer.text(sup.getKassa());
			}
			serializer.endTag(null, "kassa");
			serializer.startTag(null, "postavkavalue");
			if (sup.getPostavkavalue() == null) {
				serializer.text("");
			} else {
				serializer.text(sup.getPostavkavalue());
			}
			serializer.endTag(null, "postavkavalue");
			
			serializer.startTag(null, "agentvalue");
			if (sup.getAgentvalue() == null) {
				serializer.text("");
			} else {
				serializer.text(sup.getAgentvalue());
			}
			serializer.endTag(null, "agentvalue");
			serializer.startTag(null, "zakupkavalue");
			if (sup.getZakupkavalue() == null) {
				serializer.text("");
			} else {
				serializer.text(sup.getZakupkavalue());
			}
			serializer.endTag(null, "zakupkavalue");
			serializer.startTag(null, "gsmvalue");
			if (sup.getGsmvalue() == null) {
				serializer.text("");
			} else {
				serializer.text(sup.getGsmvalue());
			}
			serializer.endTag(null, "gsmvalue");
			serializer.startTag(null, "oworkervalue");
			if (sup.getOworkervalue() == null) {
				serializer.text("");
			} else {
				serializer.text(sup.getOworkervalue());
			}
			serializer.endTag(null, "oworkervalue");
			
			serializer.startTag(null, "prazvalue");
			if (sup.getPrazvalue() == null) {
				serializer.text("");
			} else {
				serializer.text(sup.getPrazvalue());
			}
			serializer.endTag(null, "prazvalue");
			
			serializer.startTag(null, "payvalue");
			if (sup.getPayvalue() == null) {
				serializer.text("");
			} else {
				serializer.text(sup.getPayvalue());
			}
			serializer.endTag(null, "payvalue");
			
			serializer.startTag(null, "bdohodvalue");
			if (sup.getBdohodvalue() == null) {
				serializer.text("");
			} else {
				serializer.text(sup.getBdohodvalue());
			}
			serializer.endTag(null, "bdohodvalue");
			
			serializer.startTag(null, "adohodvalue");
			if (sup.getAdohodvalue() == null) {
				serializer.text("");
			} else {
				serializer.text(sup.getAdohodvalue());
			}
			serializer.endTag(null, "adohodvalue");
			
			serializer.startTag(null, "zdohodvalue");
			if (sup.getZdohodvalue() == null) {
				serializer.text("");
			} else {
				serializer.text(sup.getZdohodvalue());
			}
			serializer.endTag(null, "zdohodvalue");
			
			serializer.startTag(null, "proizvodstvovalue");
			if (sup.getProizvodstvovalue() == null) {
				serializer.text("");
			} else {
				serializer.text(sup.getProizvodstvovalue());
			}
			serializer.endTag(null, "proizvodstvovalue");
			
			serializer.startTag(null, "stroikavalue");
			if (sup.getStroikavalue() == null) {
				serializer.text("");
			} else {
				serializer.text(sup.getStroikavalue());
			}
			serializer.endTag(null, "stroikavalue");
			
			serializer.startTag(null, "officevalue");
			if (sup.getOfficevalue() == null) {
				serializer.text("");
			} else {
				serializer.text(sup.getOfficevalue());
			}
			serializer.endTag(null, "officevalue");
			
			serializer.startTag(null, "autovalue");
			if (sup.getAutovalue() == null) {
				serializer.text("");
			} else {
				serializer.text(sup.getAutovalue());
			}
			serializer.endTag(null, "autovalue");
			
			serializer.startTag(null, "toolsvalue");
			if (sup.getToolsvalue() == null) {
				serializer.text("");
			} else {
				serializer.text(sup.getToolsvalue());
			}
			serializer.endTag(null, "toolsvalue");
			
			serializer.startTag(null, "othervalue");
			if (sup.getOthervalue() == null) {
				serializer.text("");
			} else {
				serializer.text(sup.getOthervalue());
			}
			serializer.endTag(null, "othervalue");
			
			serializer.startTag(null, "distributors");
			if(sup.getDistributors().size() == 0){
				serializer.text("\n");
			}
			for (int i = 0; i < sup.getDistributors().size(); i++) {
				serializer.startTag(null, "distributor");
				
				serializer.startTag(null, "postname");
				serializer.text((String) sup.getDistributors().get(i).getKey());
				serializer.endTag(null, "postname");
				serializer.startTag(null, "postcode");
				serializer.text((String) sup.getDistributors().get(i).getValue1());
				serializer.endTag(null, "postcode");
				serializer.startTag(null, "postsumm");
				serializer.text((String) sup.getDistributors().get(i).getValue2());
				serializer.endTag(null, "postsumm");
				
				serializer.endTag(null, "distributor");
			}
			serializer.endTag(null, "distributors");
			
			serializer.startTag(null, "agents");
			if(sup.getAgents().size() == 0){
				serializer.text("\n");
			}
			for (int i = 0; i < sup.getAgents().size(); i++) {
				serializer.startTag(null, "agent");
				
				serializer.startTag(null, "agname");
				serializer.text((String) sup.getAgents().get(i).getKey());
				serializer.endTag(null, "agname");
				serializer.startTag(null, "agcode");
				serializer.text((String) sup.getAgents().get(i).getValue1());
				serializer.endTag(null, "agcode");
				serializer.startTag(null, "agsumm");
				serializer.text((String) sup.getAgents().get(i).getValue2());
				serializer.endTag(null, "agsumm");
				
				serializer.endTag(null, "agent");
			}
			serializer.endTag(null, "agents");
			
			serializer.startTag(null, "prazs");
			if(sup.getPrazs().size() == 0){
				serializer.text("\n");
			}
			for (int i = 0; i < sup.getPrazs().size(); i++) {
				serializer.startTag(null, "praz");
				
				serializer.startTag(null, "prazname");
				serializer.text((String) sup.getPrazs().get(i).getKey());
				serializer.endTag(null, "prazname");
				serializer.startTag(null, "prazsumm");
				serializer.text((String) sup.getPrazs().get(i).getValue());
				serializer.endTag(null, "prazsumm");
				
				serializer.endTag(null, "praz");
			}
			serializer.endTag(null, "prazs");
			
			serializer.startTag(null, "pays");
			if(sup.getPays().size() == 0){
				serializer.text("\n");
			}
			for (int i = 0; i < sup.getPays().size(); i++) {
				serializer.startTag(null, "pay");
				
				serializer.startTag(null, "payname");
				serializer.text((String) sup.getPays().get(i).getKey());
				serializer.endTag(null, "payname");
				serializer.startTag(null, "paycode");
				serializer.text((String) sup.getPays().get(i).getValue1());
				serializer.endTag(null, "paycode");
				serializer.startTag(null, "paysumm");
				serializer.text((String) sup.getPays().get(i).getValue2());
				serializer.endTag(null, "paysumm");
				
				serializer.endTag(null, "pay");
			}
			serializer.endTag(null, "pays");
			
			
			serializer.startTag(null, "bdohods");
			if(sup.getBdohods().size() == 0){
				serializer.text("\n");
			}
			for (int i = 0; i < sup.getBdohods().size(); i++) {
				serializer.startTag(null, "bdohod");
				
				serializer.startTag(null, "bdohodname");
				serializer.text((String) sup.getBdohods().get(i).getKey());
				serializer.endTag(null, "bdohodname");
				serializer.startTag(null, "bdohoddate");
				serializer.text((String) sup.getBdohods().get(i).getValue1());
				serializer.endTag(null, "bdohoddate");
				serializer.startTag(null, "bdohodsumm");
				serializer.text((String) sup.getBdohods().get(i).getValue2());
				serializer.endTag(null, "bdohodsumm");
				
				serializer.endTag(null, "bdohod");
			}
			serializer.endTag(null, "bdohods");
			
			
			serializer.startTag(null, "adohods");
			if(sup.getAdohods().size() == 0){
				serializer.text("\n");
			}
			for (int i = 0; i < sup.getAdohods().size(); i++) {
				serializer.startTag(null, "adohod");
				
				serializer.startTag(null, "adohodname");
				serializer.text((String) sup.getAdohods().get(i).getKey());
				serializer.endTag(null, "adohodname");
				serializer.startTag(null, "adohodcode");
				serializer.text((String) sup.getAdohods().get(i).getValue1());
				serializer.endTag(null, "adohodcode");
				serializer.startTag(null, "adohodsumm");
				serializer.text((String) sup.getAdohods().get(i).getValue2());
				serializer.endTag(null, "adohodsumm");
				serializer.startTag(null, "adohoddate");
				serializer.text((String) sup.getAdohods().get(i).getValue3());
				serializer.endTag(null, "adohoddate");
				
				serializer.endTag(null, "adohod");
			}
			serializer.endTag(null, "adohods");
			
			
			
			serializer.startTag(null, "zdohods");
			if(sup.getZdohods().size() == 0){
				serializer.text("\n");
			}
			for (int i = 0; i < sup.getZdohods().size(); i++) {
				serializer.startTag(null, "zdohod");
				
				serializer.startTag(null, "zdohodnumber");
				serializer.text((String) sup.getZdohods().get(i).getKey());
				serializer.endTag(null, "zdohodnumber");
				serializer.startTag(null, "zdohodsumm");
				serializer.text((String) sup.getZdohods().get(i).getValue1());
				serializer.endTag(null, "zdohodsumm");
				serializer.startTag(null, "zdohoddate");
				serializer.text((String) sup.getZdohods().get(i).getValue2());
				serializer.endTag(null, "zdohoddate");
				
				serializer.endTag(null, "zdohod");
			}
			serializer.endTag(null, "zdohods");
			
			
			serializer.startTag(null, "oworkers");
			if(sup.getOworkers().size() == 0){
				serializer.text("\n");
			}
			for (int i = 0; i < sup.getOworkers().size(); i++) {
				serializer.startTag(null, "oworker");
				
				serializer.startTag(null, "oworkername");
				serializer.text((String) sup.getOworkers().get(i).getKey());
				serializer.endTag(null, "oworkername");
				serializer.startTag(null, "oworkerdate");
				serializer.text((String) sup.getOworkers().get(i).getValue1());
				serializer.endTag(null, "oworkerdate");
				serializer.startTag(null, "oworkersumm");
				serializer.text((String) sup.getOworkers().get(i).getValue2());
				serializer.endTag(null, "oworkersumm");
				
				serializer.endTag(null, "oworker");
			}
			serializer.endTag(null, "oworkers");
			
			serializer.startTag(null, "manufactures");
			if(sup.getManufactures().size() == 0){
				serializer.text("\n");
			}
			for (int i = 0; i < sup.getManufactures().size(); i++) {
				serializer.startTag(null, "manufacture");			
				serializer.startTag(null, "manuname");
				serializer.text((String) sup.getManufactures().get(i).getManuname());
				serializer.endTag(null, "manuname");
				serializer.startTag(null, "manucode");
				serializer.text((String) sup.getManufactures().get(i).getManucode());
				serializer.endTag(null, "manucode");
				serializer.startTag(null, "manuunit");
				serializer.text((String) sup.getManufactures().get(i).getManuunit());
				serializer.endTag(null, "manuunit");
				serializer.startTag(null, "manuunitcode");
				serializer.text((String) sup.getManufactures().get(i).getManuunitcode());
				serializer.endTag(null, "manuunitcode");
				serializer.startTag(null, "manuamount");
				serializer.text((String) sup.getManufactures().get(i).getManuamount());
				serializer.endTag(null, "manuamount");
				serializer.startTag(null, "manuprice");
				serializer.text((String) sup.getManufactures().get(i).getManuprice());
				serializer.endTag(null, "manuprice");
				serializer.startTag(null, "manusumm");
				serializer.text((String) sup.getManufactures().get(i).getManusumm());
				serializer.endTag(null, "manusumm");
				serializer.startTag(null, "manusummdrob");
				serializer.text((String) sup.getManufactures().get(i).getManusummdrob());
				serializer.endTag(null, "manusummdrob");
				
				serializer.endTag(null, "manufacture");
			}
			serializer.endTag(null, "manufactures");
			
			serializer.startTag(null, "builders");
			if(sup.getBuilders().size() == 0){
				serializer.text("\n");
			}
			for (int i = 0; i < sup.getBuilders().size(); i++) {
				serializer.startTag(null, "builder");			
				serializer.startTag(null, "bgroupcode");
				serializer.text((String) sup.getBuilders().get(i).getBgroupcode());
				serializer.endTag(null, "bgroupcode");
				serializer.startTag(null, "builname");
				serializer.text((String) sup.getBuilders().get(i).getBuilname());
				serializer.endTag(null, "builname");
				serializer.startTag(null, "builcode");
				serializer.text((String) sup.getBuilders().get(i).getBuilcode());
				serializer.endTag(null, "builcode");
				serializer.startTag(null, "builunit");
				serializer.text((String) sup.getBuilders().get(i).getBuilunit());
				serializer.endTag(null, "builunit");
				serializer.startTag(null, "builunitcode");
				serializer.text((String) sup.getBuilders().get(i).getBuilunitcode());
				serializer.endTag(null, "builunitcode");
				serializer.startTag(null, "builamount");
				serializer.text((String) sup.getBuilders().get(i).getBuilamount());
				serializer.endTag(null, "builamount");
				serializer.startTag(null, "builprice");
				serializer.text((String) sup.getBuilders().get(i).getBuilprice());
				serializer.endTag(null, "builprice");
				serializer.startTag(null, "builsumm");
				serializer.text((String) sup.getBuilders().get(i).getBuilsumm());
				serializer.endTag(null, "builsumm");
				serializer.startTag(null, "builsummdrob");
				serializer.text((String) sup.getBuilders().get(i).getBuilsummdrob());
				serializer.endTag(null, "builsummdrob");
				
				serializer.endTag(null, "builder");
			}
			serializer.endTag(null, "builders");
			
			serializer.startTag(null, "autocars");
			if(sup.getAutocars().size() == 0){
				serializer.text("\n");
			}
			for (int i = 0; i < sup.getAutocars().size(); i++) {
				serializer.startTag(null, "autocar");			
				serializer.startTag(null, "agroupcode");
				serializer.text((String) sup.getAutocars().get(i).getAgroupcode());
				serializer.endTag(null, "agroupcode");
				serializer.startTag(null, "autoname");
				serializer.text((String) sup.getAutocars().get(i).getAutoname());
				serializer.endTag(null, "autoname");
				serializer.startTag(null, "autocode");
				serializer.text((String) sup.getAutocars().get(i).getAutocode());
				serializer.endTag(null, "autocode");
				serializer.startTag(null, "autounit");
				serializer.text((String) sup.getAutocars().get(i).getAutounit());
				serializer.endTag(null, "autounit");
				serializer.startTag(null, "autounitcode");
				serializer.text((String) sup.getAutocars().get(i).getAutounitcode());
				serializer.endTag(null, "autounitcode");
				serializer.startTag(null, "autoamount");
				serializer.text((String) sup.getAutocars().get(i).getAutoamount());
				serializer.endTag(null, "autoamount");
				serializer.startTag(null, "autoprice");
				serializer.text((String) sup.getAutocars().get(i).getAutoprice());
				serializer.endTag(null, "autoprice");
				serializer.startTag(null, "autosumm");
				serializer.text((String) sup.getAutocars().get(i).getAutosumm());
				serializer.endTag(null, "autosumm");
				serializer.startTag(null, "autosummdrob");
				serializer.text((String) sup.getAutocars().get(i).getAutosummdrob());
				serializer.endTag(null, "autosummdrob");
				
				serializer.endTag(null, "autocar");
			}
			serializer.endTag(null, "autocars");
			
			serializer.startTag(null, "gsms");
			if(sup.getGsms().size() == 0){
				serializer.text("\n");
			}
			for (int i = 0; i < sup.getGsms().size(); i++) {
				serializer.startTag(null, "gsm");
				
				serializer.startTag(null, "gsmdate");
				serializer.text((String) sup.getGsms().get(i).getKey());
				serializer.endTag(null, "gsmdate");
				serializer.startTag(null, "gsmsumm");
				serializer.text((String) sup.getGsms().get(i).getValue());
				serializer.endTag(null, "gsmsumm");
				
				serializer.endTag(null, "gsm");
			}
			serializer.endTag(null, "gsms");
			
			serializer.startTag(null, "offices");
			if(sup.getOffices().size() == 0){
				serializer.text("\n");
			}
			for (int i = 0; i < sup.getOffices().size(); i++) {
				serializer.startTag(null, "office");			
				serializer.startTag(null, "officegroupcode");
				serializer.text((String) sup.getOffices().get(i).getOfficegroupcode());
				serializer.endTag(null, "officegroupcode");
				serializer.startTag(null, "officename");
				serializer.text((String) sup.getOffices().get(i).getOfficename());
				serializer.endTag(null, "officename");
				serializer.startTag(null, "officecode");
				serializer.text((String) sup.getOffices().get(i).getOfficecode());
				serializer.endTag(null, "officecode");
				serializer.startTag(null, "officeunit");
				serializer.text((String) sup.getOffices().get(i).getOfficeunit());
				serializer.endTag(null, "officeunit");
				serializer.startTag(null, "officeunitcode");
				serializer.text((String) sup.getOffices().get(i).getOfficeunitcode());
				serializer.endTag(null, "officeunitcode");
				serializer.startTag(null, "officeamount");
				serializer.text((String) sup.getOffices().get(i).getOfficeamount());
				serializer.endTag(null, "officeamount");
				serializer.startTag(null, "officeprice");
				serializer.text((String) sup.getOffices().get(i).getOfficeprice());
				serializer.endTag(null, "officeprice");
				serializer.startTag(null, "officesumm");
				serializer.text((String) sup.getOffices().get(i).getOfficesumm());
				serializer.endTag(null, "officesumm");
				serializer.startTag(null, "officesummdrob");
				serializer.text((String) sup.getOffices().get(i).getOfficesummdrob());
				serializer.endTag(null, "officesummdrob");
				
				serializer.endTag(null, "office");
			}
			serializer.endTag(null, "offices");
			
			serializer.startTag(null, "tools");
			if(sup.getTools().size() == 0){
				serializer.text("\n");
			}
			for (int i = 0; i < sup.getTools().size(); i++) {
				serializer.startTag(null, "tool");	
				
				serializer.startTag(null, "toolgroupcode");
				serializer.text((String) sup.getTools().get(i).getToolgroupcode());
				serializer.endTag(null, "toolgroupcode");
				serializer.startTag(null, "toolname");
				serializer.text((String) sup.getTools().get(i).getToolname());
				serializer.endTag(null, "toolname");
				serializer.startTag(null, "toolcode");
				serializer.text((String) sup.getTools().get(i).getToolcode());
				serializer.endTag(null, "toolcode");
				serializer.startTag(null, "toolunit");
				serializer.text((String) sup.getTools().get(i).getToolunit());
				serializer.endTag(null, "toolunit");
				serializer.startTag(null, "toolunitcode");
				serializer.text((String) sup.getTools().get(i).getToolunitcode());
				serializer.endTag(null, "toolunitcode");
				serializer.startTag(null, "toolamount");
				serializer.text((String) sup.getTools().get(i).getToolamount());
				serializer.endTag(null, "toolamount");
				serializer.startTag(null, "toolprice");
				serializer.text((String) sup.getTools().get(i).getToolprice());
				serializer.endTag(null, "toolprice");
				serializer.startTag(null, "toolsumm");
				serializer.text((String) sup.getTools().get(i).getToolsumm());
				serializer.endTag(null, "toolsumm");
				serializer.startTag(null, "toolsummdrob");
				serializer.text((String) sup.getTools().get(i).getToolsummdrob());
				serializer.endTag(null, "toolsummdrob");
				
				serializer.endTag(null, "tool");
			}
			serializer.endTag(null, "tools");
			
			serializer.startTag(null, "others");
			if(sup.getOthers().size() == 0){
				serializer.text("\n");
			}
			for (int i = 0; i < sup.getOthers().size(); i++) {
				serializer.startTag(null, "other");
				
				serializer.startTag(null, "othercode");
				serializer.text((String) sup.getOthers().get(i).getKey());
				serializer.endTag(null, "othercode");
				serializer.startTag(null, "othername");
				serializer.text((String) sup.getOthers().get(i).getValue1());
				serializer.endTag(null, "othername");
				serializer.startTag(null, "otherdesc");
				serializer.text((String) sup.getOthers().get(i).getValue2());
				serializer.endTag(null, "otherdesc");
				serializer.startTag(null, "othersumm");
				serializer.text((String) sup.getOthers().get(i).getValue3());
				serializer.endTag(null, "othersumm");
				
				serializer.endTag(null, "other");
			}
			serializer.endTag(null, "others");
			
			serializer.endTag(null, "data");
			serializer.endDocument();
			serializer.flush();
			fileos.close();
		} catch (Exception e) {
			Log.e("Exception", "Exception occured in wroting");
			return null;
		}
		return xmlFile;
	}
	public static boolean deleteList(String file ){

			File pFile = new File(IOHandler.getFilePath(file));
			
			if (pFile.exists()) {
				if(pFile.delete()){
			        return true;}
				else{
					return false;
				}
			}
			else{
			return true;
			}
		}
	
}
